package study.stream.文件生成流;

import study.User;
import study.utils.JsonUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author 刘伟涛
 * @Date 2024/1/31 16:04
 **/
public class Demo02 {
    public static void main(String[] args) throws IOException {
        File file = new File("D://gongxiang");
        File[] files = file.listFiles();
        List<HashMap> fileList = new LinkedList<>();
        for (File file1 : files){
            String name = file1.getName();
            FileInputStream fis = new FileInputStream("D://gongxiang/" + name);
            byte[] data = new byte[1024*10];//10kb
            String line;
            int len = fis.read(data);//将读到的字节装到字节数组中
            System.out.println("总共读取的大小"+len);//实际大小
            line = new String(data,0,len, StandardCharsets.UTF_8);//将字节数组转换成指定的字符串类型（字符集类型）
            System.out.println(line);

            HashMap map = new HashMap();
            map.put("fileName",name);
            map.put("fileContent",line);
            fileList.add(map);
            fis.close();
        }

        if (file.exists()){
            for (File file1 : files){
                boolean delete = file1.delete();
                System.out.println(delete);
            }
        }
        System.out.println(JsonUtils.objectToJson(fileList));
    }
}
